<script setup lang="ts" name="GlobalReferenceAddEdit">
import { nextId } from '../../../utils/id'
const AddEditFormRef = ref(null)

const emit = defineEmits(['save'])

const visible = ref(false)
const title = ref('')
const type = ref('')
const typeText = ref('')

const formData = ref({
  name: '',
  id: '',
  errorCode: '',
  errorMessage: ''
})

const rules = ref({
  name: { required: true, message: typeText.value + '名称不能为空', trigger: ['blur', 'change'] },
  id: { required: true, message: typeText.value + 'ID不能为空', trigger: ['blur', 'change'] }
})

// 显示新建/编辑部门弹窗
const showDialog = (v) => {
  visible.value = true
  type.value = v.type
  typeText.value = v.text
  if (v.row) {
    title.value = '编辑' + v.text + '参考项'
    formData.value.name = v.row.name
    formData.value.id = v.row.id
    if (type.value === 'error') {
      formData.value.errorCode = v.row.errorCode
      formData.value.errorMessage = v.row.errorMessage
    }
  } else {
    title.value = '添加' + v.text + '参考项'
    formData.value.id = formData.value.name = nextId(getNextIdPrefix())
  }
}
// 子组件将方法暴露给父组件
defineExpose({ showDialog })

const closeDialog = () => {
  formData.value.name = ''
  formData.value.id = ''
  formData.value.errorCode = ''
  formData.value.errorMessage = ''
  if (AddEditFormRef.value) {
    AddEditFormRef.value.clearValidate()
  }
  visible.value = false
}

const handleOk = () => {
  if (!AddEditFormRef.value) return false
  AddEditFormRef.value.validate((valid) => {
    if (valid) {
      const result: any = {}
      result.name = formData.value.name
      result.id = formData.value.id
      if (type.value === 'error') {
        result.errorCode = formData.value.errorCode
        result.errorMessage = formData.value.errorMessage
      }
      emit('save', result)
      closeDialog()
    }
  })
}

const getNextIdPrefix = () => {
  if (type.value === 'error') {
    return 'Error_'
  } else if (type.value === 'message') {
    return 'Message_'
  } else if (type.value === 'signal') {
    return 'Signal_'
  } else if (type.value === 'escalation') {
    return 'Escalation_'
  }
}
</script>

<template>
  <el-dialog v-model="visible" width="600px" append-to-body destroy-on-close :title="title">
    <el-form ref="AddEditFormRef" label-width="auto" label-position="right" :model="formData" :rules="rules">
      <el-form-item :label="typeText + 'ID'" prop="id">
        <el-input v-model="formData.id" clearable readonly disabled :placeholder="'请输入' + typeText + 'ID'" />
      </el-form-item>
      <el-form-item :label="typeText + '名称'" prop="name">
        <el-input v-model="formData.name" clearable :placeholder="'请输入' + typeText + '名称'" />
      </el-form-item>
      <template v-if="type === 'error'">
        <el-form-item label="错误码" prop="errorCode" :rules="{ required: true, trigger: ['blur', 'change'], message: '错误码不能为空' }">
          <el-input v-model="formData.errorCode" />
        </el-form-item>
        <el-form-item label="错误信息" prop="errorMessage" :rules="{ required: true, trigger: ['blur', 'change'], message: '错误信息不能为空' }">
          <el-input v-model="formData.errorMessage" />
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <span class="modal-footer" ref="modalFooter">
        <el-button @click="closeDialog">{{ $t('buttons.buttonCancel') }}</el-button>
        <el-button type="primary" @click="handleOk">{{ $t('buttons.buttonConfirm') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-form {
  padding: 40px 80px;
}

.modal-footer{
  display: inline-flex;
  overflow: hidden;
}
</style>
